package net.zjitc.demosecurity6.service.impl;


import net.zjitc.demosecurity6.mapper.MenuMapper;
import net.zjitc.demosecurity6.mapper.UserMapper;
import net.zjitc.demosecurity6.pojo.LoginUser;
import net.zjitc.demosecurity6.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zhao
 * @DATE 2025/4/25 11:01
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 根据用户名查询用户信息
        User user = userMapper.getUserByName(username);
        if (user == null) {
            // 如果用户不存在，抛出异常
            throw new UsernameNotFoundException("用户不存在");
        }
        // 根据用户信息查询其权限列表
        List<String> permsList= menuMapper.getPermsByUserId(user.getId());

        // 封装权限列表到UserDetails中

        return new LoginUser(user,permsList);
    }
}
